import type { LoginResult, PermissionResult, SignInCaptchaResult, SignInReq, TokenResult } from "@/api/types";
import { userStorage } from "@/config/storage";
import request from "@/utils/http";

/** 用户名账号登录 */
const signInByUsername = (data: SignInReq): Promise<Resp<LoginResult>> => {
    return request({
        url: "/auth/signInByUsername",
        method: "post",
        data,
    });
};

/** 获取登录验证码 */
const getSignInCaptcha = (): Promise<Resp<SignInCaptchaResult>> => {
    return request({
        url: "/auth/getSignInCaptcha",
        method: "get",
    });
};

/** 刷新token */
const refreshAccessToken = (refreshToken: string): Promise<Resp<TokenResult>> => {
    return request({
        url: "/auth/refreshAccessToken",
        method: "post",
        data: {
            refreshToken,
        },
    });
};

/** 根据角色列表获取权限列表 */
const findPermissions = (): Promise<Resp<PermissionResult>> => {
    return request({
        url: `/auth/findPermissions`,
        method: "post",
        data: {
            roles: userStorage.value?.roles ?? [],
        },
    });
};

/** 认证相关接口 */
export const authApi = {
    signInByUsername,
    getSignInCaptcha,
    refreshAccessToken,
    findPermissions,
};
